package me.abel.quartz.service;

import com.alibaba.fastjson.JSON;
import me.abel.quartz.dto.AccessToken;
import me.abel.quartz.util.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;

public interface AccessTokenService {

    /**
     * 定时刷新AccessToken
     */
    void refresh(String corpId, String appSecret);

    /**
     * 获取AccessToken
     * @param corpId
     * @return
     */
    default AccessToken getRemote(String corpId, String secret) {
        String accessTokenUri = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=APPID&corpsecret=SECRET";
        accessTokenUri = accessTokenUri.replace("APPID", corpId);
        accessTokenUri = accessTokenUri.replace("SECRET", secret);

        String tokenJson = HttpClientUtil.getResponce(accessTokenUri);
        if (StringUtils.isNotBlank(tokenJson)) {
            AccessToken token = JSON.parseObject(tokenJson, AccessToken.class);
            if (StringUtils.isNotBlank(token.getAccessToken())) {
                token.init();
                return token;
            }
        }
        return null;
    }

}
